import { useReducer } from 'react';

/** 定义初始状态信息  */
interface IstateType {
  // 第二步骤条时，需要supplyKind&&supplyType两个字段作为添加包的内容
  supplyTK: Record<string, any>;
  // 当前所在包
  page: number;
  // 保存项目的信息，便于在第二步骤中，添加实施信息时使用
  ProjectInfo: Record<string, any>;
}

/** reducer 枚举类型 */
enum IContractsActionEnum {
  UpdateSupplyTK = 'UpdateSupplyTK',
  UpdatePage = 'UpdatePage',
  UpdataProJect = 'UpdataProJect',
  UPDATE_DATASOURCE = 'update_dataSource',
}

/**
 * 定义页面操作匹配
 * @type 操作类型
 * @payload 传递数据
 */
interface IActionType {
  type: IContractsActionEnum;
  payload?: any;
}

/** 状态仓库 */
const useContractsReducer = () => {
  /** 初始化状态 */
  const initState: IstateType = {
    supplyTK: {},
    page: 0,
    ProjectInfo: {},
  };

  /** 状态管理逻辑处理 */
  const updateReducer = (state: IstateType, action: IActionType) => {
    switch (action?.type) {
      case IContractsActionEnum.UpdateSupplyTK:
        return {
          ...state,
          supplyTK: action.payload,
        };
      case IContractsActionEnum.UpdatePage:
        return {
          ...state,
          page: action.payload,
        };
      case IContractsActionEnum.UpdataProJect:
        return {
          ...state,
          ProjectInfo: action.payload,
        };
      case IContractsActionEnum.UPDATE_DATASOURCE:
        return {
          ...state,
          dataSource: action?.payload,
        };
      default:
        return {
          ...state,
          ...initState,
        };
    }
  };

  const [state, dispatch] = useReducer(updateReducer, initState);

  return {
    ...state,
    dispatch,
  };
};

export { useContractsReducer, IContractsActionEnum };
